var prefix = "/sys/menu"
$(function() {
	validateRule();
	//打开图标列表
    $("#ico-btn").click(function(){
        layer.open({
            type: 2,
			title:'图标列表',
            content: '/FontIcoList.html',
            area: ['480px', '90%'],
            success: function(layero, index){
                //var body = layer.getChildFrame('.ico-list', index);
                //console.log(layero, index);
            }
        });
    });
	// 上传
	layui.use('upload', function () {
		var upload = layui.upload;
		//执行实例
		upload.render({
			elem: '#btnUpload', //绑定元素
			url: '/jzApi/utils/fileImprot', //上传接口
			accept: 'file',
			done: function (r) {
				$('#imgAll').html(`
<img src="${r.data}" id="imgFil"  style="max-width: 50%;height: auto;cursor: pointer;" />`)
			},
			error: function (r) {
				layer.msg(r.msg);
			}
		});
	});
});
$.validator.setDefaults({
	submitHandler : function() {
		submit01();
	}
});
function submit01() {
	let imgs = $('#imgAll').children().length
	if(imgs === 0) {
		layer.msg("请上传资源封面")
		return
	}
	let img = $('#imgFil').attr('src')
	let formData = $('#signupForm').serializeArray();
	let  data = {};
	$.each(formData, function(index, item) {
		// .replace(item.name[0],item.name[0].toUpperCase())
		data[item.name] = item.value;

	});
	// data.newType === '' ?  data['publicity'] = 1 : data['publicity'] = 0
	data['img'] = img


	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/save",
		data : data,
		async : false,
		error : function(request) {
			laryer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("保存成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				layer.alert(data.msg)
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			type : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入菜单名"
			},
			type : {
				required : icon + "请选择菜单类型"
			}
		}
	})
}